package com.icen.utils.comm;

import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;

/**
 * Animation Utils Class
 * 
 * @author IcenHan
 * 
 */
public class AnimatUtils {
	/**
	 * 动画:淡入
	 * 
	 * @param view
	 */
	public static void alphaIn(View view) {
		AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);
		aa.setDuration(2000);
		view.setAnimation(aa);
		view.startAnimation(aa);
	}

	/**
	 * 动画:淡出
	 * 
	 * @param view
	 */
	public static void alphaOut(View view) {
		AlphaAnimation aa = new AlphaAnimation(1.0f, 0f);
		aa.setDuration(2000);
		view.setAnimation(aa);
		view.startAnimation(aa);
	}

	/**
	 * 动画:摇摆
	 * 
	 * @param view
	 * @param animationsListener
	 */
	public static void swing(View view, AnimationListener animationsListener) {
		TranslateAnimation ta = translate();
		ta.setAnimationListener(animationsListener);
		view.startAnimation(ta);
	}

	/**
	 * 动画:摇摆
	 * 
	 * @param view
	 */
	public static void swing(View view) {
		TranslateAnimation ta = translate();
		view.startAnimation(ta);
	}

	private static TranslateAnimation translate() {
		TranslateAnimation ta = new TranslateAnimation(
				Animation.RELATIVE_TO_SELF, -0.5f, Animation.RELATIVE_TO_SELF,
				0.5f, Animation.RELATIVE_TO_SELF, 0.0f,
				Animation.RELATIVE_TO_SELF, 0.0f);
		ta.setDuration(200);
		return ta;
	}

	/**
	 * 动画:渐入+旋转
	 * 
	 * @param view
	 */
	public static void apphaAndScaleIn(View view) {
		view.setVisibility(View.VISIBLE);
		AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);
		aa.setDuration(1000);
		ScaleAnimation sa = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f);
		sa.setDuration(1000);
		AnimationSet set = new AnimationSet(false);
		set.addAnimation(sa);
		set.addAnimation(aa);
		view.setAnimation(set);
		view.startAnimation(set);
	}

	/**
	 * 动画:渐出+旋转
	 * 
	 * @param view
	 */
	public static void apphaAndScaleOut(View view) {
		AlphaAnimation aa = new AlphaAnimation(1.0f, 0.0f);
		aa.setDuration(1000);
		ScaleAnimation sa = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f);
		sa.setDuration(1000);
		AnimationSet set = new AnimationSet(false);
		set.addAnimation(sa);
		set.addAnimation(aa);
		view.setAnimation(set);
		view.startAnimation(set);
		view.setVisibility(View.INVISIBLE);
	}

}
